package cn.jimmie.learn.mock;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;

import cn.jimmie.learn.pbc.resource.Plugin;
import cn.jimmie.learn.pbc.resource.PluginConst;
import cn.jimmie.learn.pbc.resource.ShareInstance;
import cn.jimmie.learn.pbc.util.Utils;
import cn.jimmie.learn.pbc.base.BaseActivity;
import cn.jimmie.learn.pbc.base.BaseFragment;


/**
 * function :
 *
 * <p> Create Time:  2019年02月12日 18:07 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class PluginHostActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sdk_fragment_activity);

        ShareInstance.instance().setPlugin(mockPlugin());
        goPluginFragment();
    }

    private void goPluginFragment() {
        Toast.makeText(this, "原生加载...", Toast.LENGTH_SHORT).show();
        BaseFragment fragment = Utils.getFragment(PluginConst.FragmentName.MAIN,
                getClassLoader());
        if (fragment == null) return;
        fragment.setContainerId(R.id.sdk_fragment_container);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.sdk_fragment_container, fragment);
        ft.commitAllowingStateLoss();
    }

    private Plugin mockPlugin() {
        return new Plugin(getClassLoader(), getAssets(), getResources());
    }
}
